# -*- coding: utf-8 -*-

import json
import collections

'''转换json到有序字典数据'''
def json2orrdeedDict(json_str):
    return json.loads(json_str, object_pairs_hook = collections.OrderedDict);
    

'''合并两个JSON'''
def mergeJson(json1, json2):
    dict1 = json.loads(json1, object_pairs_hook = collections.OrderedDict);
    dict2 = json.loads(json2, object_pairs_hook = collections.OrderedDict);
                
    dictMerged = dict(dict1, **dict2)
    jsonMerged = json.JSONEncoder().encode(dictMerged)
    
    return jsonMerged

'''获取分类图片地址'''
def getCategoryDisplaySrc(html):
    try:
        return eval(html, type('Dummy', (dict,), dict(__getitem__=lambda s, n: n))())[0]['displaySrc']
    except:
        print '---'
        return ''

'''保存json到本地文件'''
def saveJsonFile(json_str, file_name):
    save_file_path = 'json/' + file_name
    save_file = open(save_file_path, "w")
    save_file.write(json_str)
    save_file.close()